package liuyang.bigeventserver.modules.article.controller;

import liuyang.bigeventserver.common.R;
import liuyang.bigeventserver.modules.article.entity.Article;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.basic.BasicBorders;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 仅为axios测试用
 * https://www.bilibili.com/video/BV14z4y1N7pg/?p=55&vd_source=8bd7b24b38e3e12c558d839b352b32f4
 *
 * @author xconf
 * @since 2024/3/5
 */
@RestController
@RequestMapping("articledemo")
@Slf4j
@Deprecated
@CrossOrigin
public class ArticleDemoController {

    private List<Article> articleList = new ArrayList<>();

    {
        Article a1 = new Article();
        a1.setTitle("医疗反复绝非砍医护收入");
        a1.setCategoryId(123l);
        a1.setCreateTime(LocalDateTime.now());
        a1.setState("已发布");
        articleList.add(a1);

        Article a2 = new Article();
        a2.setTitle("中国男篮缘何一败涂地");
        a2.setCategoryId(123l);
        a2.setCreateTime(LocalDateTime.now());
        a2.setState("草稿");
        articleList.add(a2);

        Article a3 = new Article();
        a3.setTitle("华山景区已受大风影响阵风达7-8级");
        a3.setCategoryId(1234l);
        a3.setCreateTime(LocalDateTime.now());
        a3.setState("已发布");
        articleList.add(a3);
    }

    @PostMapping("/add")
    public R add(@RequestBody Article article) {
        log.info("article = {}", article);
        return R.ok("添加成功");
    }

    @GetMapping("/list")
    public R list() {
        return R.ok().put("data", articleList);
    }

    @GetMapping("/serch")
    public R search(Long category, String state) {
        log.debug("catetory = {}, state = {}", category, state);
        return R.ok().put("data", articleList.stream()
                .filter(a -> a.getCategoryId().equals(category) && a.getState().equals(state))
                .collect(Collectors.toList()));
    }
}
